package com.atguigu.servicebase;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import org.springframework.core.env.Environment;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig
{
    @Bean
    public Docket webApiConfig(Environment environment)
    {
        //设置要显示的swagger2环境
        Profiles profiles = Profiles.of("dev","prod");


        //通过environment.acceptsProfiles(profiles)判断是否处在自己设定的环境中
        //如果环境是dev,那就是true，如果是prod就是false
        boolean environmentFlag = environment.acceptsProfiles(profiles);


        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("苏七")
                .apiInfo(apiInfo())
                .select()
                //该路径包含admin以及error,就不显示
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();
    }
    //配置apiInfo
    public ApiInfo apiInfo()
    {
        /*作者信息*/
        Contact contact = new Contact("苏七","http://suqiqaq.cn","1141793961@qq.com");

        return new ApiInfo("blog-api Documentation",
                "课程中心API文档",
                "v1.0",
                "http://suqiqaq.cn",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }

}